<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="root">
        <button id="btnTest">测试的按钮</button>
    </div>
    <div>
        <button type="button" id="btnRemove">删除按钮</button>
    </div>

    <script>

        ; (function () {

            var btnTest = document.getElementById("btnTest");

            function onClick() {
                console.log("被点击了")
            }

            const wkRef = new WeakRef(onClick);

            btnTest.addEventListener("click", onClick);
            btnRemove.addEventListener("click", function () {
                root.innerHTML = "";
            })

            // 哦豁
            console.log("btnTest", btnTest)
            setInterval(() => {
                if (wkRef.deref()) {
                    console.log("onClick未回收")
                } else {
                    console.log("onClick已回收")
                }
            }, 2000);

        })();

    </script>

</body>

</html>